<?php

require_once 'classes/LightPressPlugin.php';

class Pagination extends LightPressPlugin {
    
    var $constructor_args = array(
        'set_title'     => 'Modify the title when paging, so that it is unique across the site',
        'title_posts'   => "When modifying the title, use this word for 'Posts'"
        );
    var $default_context = LP_CONTEXT_ALL;
    var $description = 'navigate through posts';
    
    var $set_title = true;
    var $title_posts = 'Posts';
    
    var $active = true;
    var $hooks = array('sidebar');
    var $_block_previous = 'plugin_pagination_prev';
    var $_block_next = 'plugin_pagination_next';
    var $page_current;
    var $page_previous;
    var $page_next;
    var $posts_previous;
    var $posts_next;
    var $url_previous;
    var $url_next;
    
    function Pagination(&$frontend, $args, $dummy_run=false) {
        $this->LightPressPlugin($frontend, $args, $dummy_run);
    }
    
    function run($hook, &$hook_data) {

        $context = $this->_frontend->context;
        $db =& $this->_frontend->db;
        $tpl =& $this->_frontend->tpl;
        
        $index = $context['index'];
        $posts_per_page = $this->_frontend->options['posts_per_page'];
        $prev_label = $prev_posts = $next_label = $next_posts = '';
        
        $tpl->setFile('plugin_pagination', 'plugins/pagination.xml', true);
        $num_posts = $this->_frontend->num_posts;
        $show_divide = true;
        $page_previous = $index - $posts_per_page;
        // echo "num_posts: $num_posts context_index: " . $context['index'] . " ppp: $posts_per_page pp: $page_previous";
        if ($index == 0) {
            $tpl->setBlock('plugin_pagination', $this->_block_previous);
            $tpl->setVar(strtoupper($this->_block_previous), '');
            $show_divide = false;
        } else if ($page_previous < 0) {
            $page_previous = 0;
        } else if ($page_previous >= $num_posts) {
            $page_previous = $num_posts - $posts_per_page;
        }
        if ($index == 1) {
            $prev_label = $this->_frontend->messages['post'];
            $prev_posts = '';
        } else {
            $prev_label = $this->_frontend->messages['posts'];
            $prev_posts = ($index > $num_posts ? $num_posts : $index);
        }
        
        $page_next = $index + $posts_per_page;
        if ($page_next >= $num_posts) {
            $tpl->setBlock('plugin_pagination', $this->_block_next);
            $tpl->setVar(strtoupper($this->_block_next), '');
            $show_divide = false;
        } else {
            if ($num_posts - $page_next == 1) {
                $next_label = $this->_frontend->messages['post'];
            } else {
                $next_label = $this->_frontend->messages['posts'];
                $next_posts = $num_posts - ($index + $posts_per_page);
            }
        }
        $this->page_current = $index;
        $this->page_previous = $page_previous;
        $this->page_next = $page_next;
        $this->posts_previous = $prev_posts;
        $this->posts_next = $next_posts;
        
        // fix title if we are on a page other than the first
        if ($this->set_title && $index > 0) {
            $tpl->setVar('title', sprintf("%s%s %s-%s &raquo; %s",
                empty($context['label']) ? '' : "{$context['label']} - ",
                $this->title_posts,
                $index + 1,
                ($page_next >= $num_posts) ? $num_posts : $page_next,
                $this->_frontend->options['blogname']));
        }
        
        if ($this->_frontend->raw_urls) {
            if (strpos($context['url'], '?') === false)
                $sep = '?';
            else
                $sep = '&amp;';
            if ($page_previous > 0)
                $this->url_previous = $prev_url = $context['url'] . "${sep}i=$page_previous";
            else
                $this->url_previous = $prev_url = $context['url'];
            $this->url_next = $next_url = $context['url'] . "${sep}i=$page_next";
        } else {
            if ($page_previous > 0)
                $this->url_previous = $prev_url = $context['url'] . "/record/$page_previous";
            else
                $this->url_previous = $prev_url = $context['url'];
            $this->url_next = $next_url = $context['url'] . "/record/$page_next";
        }
        
        $tpl->setVar(array(
            'context_url'                       => $context['url'],
            'plugin_pagination_prev_url'        => $prev_url,
            'plugin_pagination_next_url'        => $next_url,
            'plugin_pagination_prev_posts'      => $prev_posts,
            'plugin_pagination_next_posts'      => $next_posts,
            'plugin_pagination_prev_index'      => $page_previous,
            'plugin_pagination_next_index'      => $page_next,
            'plugin_pagination_prev_label'      => $prev_label,
            'plugin_pagination_next_label'      => $next_label,
            'plugin_pagination_divide'          => ($show_divide) ? '|' : ''));
        $tpl->parse('PLUGIN_PAGINATION', 'plugin_pagination');

    }
    
}

?>